package com.jml.juc.lock.base;

import java.util.concurrent.TimeUnit;
import java.util.Collection;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class ReentrantLockDemo implements Lock, java.io.Serializable {
    private static final long serialVersionUID = 7373984872572414699L;
    private final  Sync sync;

    abstract static class Sync extends AbstractQueuedSynchronizerDemo {
        private static final long serialVersionUID = -5179523762034025860L;
        abstract void lock();
        final boolean nonfairTryAcquire(int acquires) {
            return false;
        }
        @Override
        protected final boolean tryRelease(int releases) {
            System.out.println("ReentrantLockDemo.Sync.tryRelease");
            return false;
        }
        @Override
        protected final boolean isHeldExclusively() {
            return getExclusiveOwnerThread() == Thread.currentThread();
        }

        final ConditionObject newCondition() {
            return new ConditionObject();
        }
        final Thread getOwner() {
            return getState() == 0 ? null : getExclusiveOwnerThread();
        }

        final int getHoldCount() {
            return isHeldExclusively() ? getState() : 0;
        }

        final boolean isLocked() {
            return getState() != 0;
        }

        private void readObject(java.io.ObjectInputStream s)
                throws java.io.IOException, ClassNotFoundException {
        }
    }

    static final class NonfairSync extends  Sync {
        private static final long serialVersionUID = 7316153563782823691L;
        @Override
        final void lock() {
        }
        @Override
        protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }
    }
    static final class FairSync extends  Sync {
        private static final long serialVersionUID = -3000897897090466540L;
        @Override
        final void lock() {
            acquire(1);
        }
        @Override
        protected final boolean tryAcquire(int acquires) {
            return false;
        }
    }
    public ReentrantLockDemo() {
        sync = new  NonfairSync();
    }
    public ReentrantLockDemo(boolean fair) {
        sync = fair ? new  FairSync() : new  NonfairSync();
    }
    @Override
    public void lock() {
        sync.lock();
    }
    @Override
    public void lockInterruptibly() throws InterruptedException {
        sync.acquireInterruptibly(1);
    }
    @Override
    public boolean tryLock() {
        return sync.nonfairTryAcquire(1);
    }
    @Override
    public boolean tryLock(long timeout, TimeUnit unit)
            throws InterruptedException {
        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
    }
    @Override
    public void unlock() {
        sync.release(1);
    }

    @Override
    public Condition newCondition() {
        return sync.newCondition();
    }
    public int getHoldCount() {
        return sync.getHoldCount();
    }
    public boolean isHeldByCurrentThread() {
        return sync.isHeldExclusively();
    }
    public boolean isLocked() {
        return sync.isLocked();
    }
    public final boolean isFair() {
        return sync instanceof  FairSync;
    }
    protected Thread getOwner() {
        return sync.getOwner();
    }
    public final boolean hasQueuedThreads() {
        return sync.hasQueuedThreads();
    }
    public final boolean hasQueuedThread(Thread thread) {
        return sync.isQueued(thread);
    }
    public final int getQueueLength() {
        return sync.getQueueLength();
    }
    protected Collection<Thread> getQueuedThreads() {
        return sync.getQueuedThreads();
    }
    public boolean hasWaiters(Condition condition) {
        return false;
    }
    public int getWaitQueueLength(Condition condition) {
        return 1;
    }
    protected Collection<Thread> getWaitingThreads(Condition condition) {
        return null;
    }
    @Override
    public String toString() {
        Thread o = sync.getOwner();
        return super.toString() + ((o == null) ?
                "[Unlocked]" :
                "[Locked by thread " + o.getName() + "]");
    }
}
